package com.example.personalaccount.controller;


import com.example.personalaccount.dto.AccountDto;
import com.example.personalaccount.entity.Account;
import com.example.personalaccount.handler.SecurityGetUserUtil;
import com.example.personalaccount.service.AccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



/**
 *  账单的导出
 *
 *
 * */
@RestController
@RequestMapping(value = "/account")
@ResponseBody
@Tag(name = "账户管理")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private SecurityGetUserUtil securityGetUserUtil;


    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @PostMapping("/update")
    @Operation(summary = "更新账户信息")
    public Account update(@RequestBody AccountDto accountDto)
    {
        return accountService.update(accountDto);
    }


    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @PostMapping("/addAccount/{function}/{account}")
    @Operation(summary = "添加账户")
    public Account addAccount(@PathVariable String function, @PathVariable String account)
    {
        return accountService.addAccount(function, account);
    }


    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @PostMapping("/remove/{function}/{account}")
    @Operation(summary = "删除账户")
    public Account remove(@PathVariable String function, @PathVariable String account)
    {
        return accountService.removeAccount(function, account);
    }


    @PreAuthorize("hasAnyRole('ADMIN','USER')")
    @GetMapping("/self")
    @Operation(summary = "获取账户")
    public Account self()
    {
        Account userId = accountService.getAccountByUserId(securityGetUserUtil.getUser().getId());
        return userId;
    }


}
